package org.exia.minisig.swing.view.navigation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.commons.lang3.ArrayUtils;
import org.exia.minisig.model.object.DescriptibleElement;
import org.exia.minisig.model.object.Interest;
import org.exia.minisig.model.object.Itinerary;
import org.exia.minisig.model.object.News;
import org.exia.minisig.model.object.Place;
import org.exia.minisig.swing.controller.HomeFramMode;
import org.exia.minisig.swing.view.home.HomeFram;

@SuppressWarnings("serial")
public class InterestEditPanel extends JPanel {
	//VAR
		private Interest element;
		private NavigationFram parent;
		
		private String data[]  = new String[]{};
		private JTextField fieldURL = new JTextField();	
		private String data2[]  = new String[]{};
		private JList<String> listeURL = new JList<>(data);
		private JList<String> listeURLurl = new JList<>(data2);
		private JTextField filedLabel;
		private JTextArea text; 
		//CONSTRUTOR
		public InterestEditPanel(DescriptibleElement _element, NavigationFram _parent){
			initializeVar(_element, _parent);
		}
		public void initializeVar(DescriptibleElement _element, NavigationFram _parent){
			this.setBackground(Color.white);
			this.parent = _parent;
			this.element = (Interest) _element;
			/*"----------------------------------------------------------------*/
			this.text = new JTextArea(element.getText());
			this.filedLabel = new JTextField(element.getLabel());
			data = new String[element.getImages().size()];
			for (int i = 0; i < element.getImages().size(); i++) {
				data[i] =  element.getImages().get(i);
			}
			data2 = new String[element.getUrls().size()];
			for (int i = 0; i < element.getUrls().size(); i++) {
				data2[i] =  element.getUrls().get(i);
			}
			/*----------------------------------------------------------------*/
			fillPanel();
			
		}
		public void fillPanel(){
			this.removeAll();
			this.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
			this.setLayout(new GridBagLayout());
			GridBagConstraints gc = new GridBagConstraints();
			gc.fill=GridBagConstraints.BOTH;
			//PARAMETER
			gc.weightx=1;
			gc.weighty=1;
			//---------------------------------------//
			//TITRE
			String textEdition = "";
			switch (parent.getMode()) {
			case ITINERARY:
				textEdition = "Edition parcours";
				break;
			case NEWS:
				textEdition = "Edition news";
				break;
			default:
				break;
			}
			gc.gridheight=1;
			gc.gridwidth= 4;
			gc.gridx=0;
			gc.gridy=0;
			gc.insets = new Insets(0, 0, 5, 0);
			JPanel panelTitre = new JPanel(new BorderLayout());
			JLabel titre = new JLabel(textEdition);
			panelTitre.setBackground(Color.white);
			titre.setBackground(Color.white);
			titre.setOpaque(true);
			titre.setVerticalAlignment(SwingConstants.CENTER);
			titre.setHorizontalAlignment(SwingConstants.CENTER);
			panelTitre.add(titre, BorderLayout.CENTER);
			this.add(panelTitre, gc);
			//--------------------------------RDC------------------------------//
			//ETAGE 01
			//label 
			gc.insets = new Insets(0, 10, 0, 0);
			gc.gridwidth = 1;
			gc.gridheight = 1;
			gc.gridx=0;
			gc.gridy=1;
			JLabel labelLabel = new JLabel("Label :");
			labelLabel.setBackground(Color.white);
			labelLabel.setOpaque(true);
			labelLabel.setHorizontalAlignment(SwingConstants.CENTER);
			this.add(labelLabel, gc);
			//field
			gc.insets = new Insets(5, 10, 5, 10);
			gc.gridwidth = 3;
			gc.gridheight = 1;
			gc.gridx=1;
			gc.gridy=1;
			JPanel panelFieldLabel = new JPanel(new BorderLayout(0,0));
			filedLabel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
			filedLabel.setMargin(new Insets(50, 0, 0, 0));
			filedLabel.setBorder(BorderFactory.createCompoundBorder(
			filedLabel.getBorder(), 
	        BorderFactory.createEmptyBorder(0, 10, 0, 10)));
			panelFieldLabel.add(filedLabel, BorderLayout.CENTER);
			this.add(panelFieldLabel, gc);
			
			//ETAGE TEXT LONG
			gc.insets = new Insets(3, 5, 3, 5);
			gc.gridwidth = 4;
			gc.gridheight = 1;
			gc.gridx=0;
			gc.gridy=2;
			//TEST
			JPanel panel = new JPanel();
			
			
			panel.setLayout(new BorderLayout());
			text = new JTextArea(element.getText());
			JScrollPane scroll = new JScrollPane(text);
			panel.add(scroll, BorderLayout.CENTER);

			//END TEST
			this.add(panel, gc);
			//ETAGE IMAGE 
			//BTN +
			gc.gridwidth = 2;
			gc.gridheight = 1;
			gc.gridx=0;
			gc.gridy=3;
			JButton btnPlus = new JButton("Ajouter image");
			btnPlus.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					JFileChooser chooser = new JFileChooser();
				    FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif","png");
				    chooser.setFileFilter(filter);
				    int returnVal = chooser.showOpenDialog(null);
				    if(returnVal == JFileChooser.APPROVE_OPTION) {
				    	String y = chooser.getSelectedFile().getName();
				    	testAdd(y);
				    	fillPanel();
				    } 
				}
			});
			
			btnPlus.setFocusPainted(false);
			this.add(btnPlus, gc);
			//BTN -
			gc.gridwidth = 2;
			gc.gridheight = 1;
			gc.gridx=2;
			gc.gridy=3;
			
			JButton btnLess = new JButton("supprimer image");
			btnLess.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					
					if (listeURL.getSelectedValue() != null) {
						
					    int z=	listeURL.getSelectedIndex();
						data = ArrayUtils.remove(data,z );
						fillPanel();
					}
					
				}
			});
			
			this.add(btnLess, gc);
			
			
			//ETAGE LISTE  URL IMAGE
			gc.insets = new Insets(3, 5, 3, 5);
			gc.gridwidth = 4;
			gc.gridheight = 3;
			gc.gridx=0;
			gc.gridy=5;
			
			this.listeURL = new JList<>(data);			
			this.listeURL.setVisibleRowCount(5);			
			JScrollPane panelListImg = new JScrollPane(this.listeURL);
			this.add(panelListImg, gc);
			
			//URL ETAGE BTN
			gc.gridwidth = 2;
			gc.gridheight = 1;
			gc.gridx=0;
			gc.gridy=8;
		//	JTextField fieldURL = new JTextField();
			
			this.fieldURL.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
			this.fieldURL.setMargin(new Insets(50, 0, 0, 0));
			this.fieldURL.setBorder(BorderFactory.createCompoundBorder(
			this.fieldURL.getBorder(), 
			BorderFactory.createEmptyBorder(0, 10, 0, 10)));
			this.add(this.fieldURL, gc);

			//ETAGE BTN
			//+
			gc.gridwidth = 1;
			gc.gridheight = 1;
			gc.gridx=2;
			gc.gridy=8;
			JButton btnPlusURL = new JButton("Ajouter URL");
			btnPlusURL.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					String x =fieldURL.getText();
					 AddUrl(x);									
				}
			});
			btnPlus.setFocusPainted(false);
			this.add(btnPlusURL, gc);
			//-
			gc.gridwidth = 1;
			gc.gridheight = 1;
			gc.gridx=3;
			gc.gridy=8;
			JButton btnLessURL = new JButton("Supprimer URL");
			btnLessURL.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					if (listeURLurl.getSelectedValue() != null) {
						
					    int z=	listeURLurl.getSelectedIndex();
						data2 = ArrayUtils.remove(data2,z );
						fillPanel();
					}
					
				}
			});
			btnPlus.setFocusPainted(false);
			this.add(btnLessURL, gc);
			
			//ETAGE LISTE  URL URL
			gc.insets = new Insets(3, 5, 3, 5);
			gc.gridwidth = 4;
			gc.gridheight = 3;
			gc.gridx=0;
			gc.gridy=9;
			
			this.listeURLurl = new JList<>(data2);
			this.listeURLurl.setVisibleRowCount(5);
			JScrollPane listeURLurl = new JScrollPane(this.listeURLurl);
			this.add(listeURLurl, gc);
			
			//ETAGE BTN
			gc.gridwidth = 2;
			gc.gridheight = 1;
			gc.gridx=0;
			gc.gridy=12;
			JButton btnSave = new JButton("Enregistrer");
			btnSave.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Interest newInterest = new Interest();
					newInterest.setLabel(filedLabel.getText());
			    	newInterest.setText(text.getText());
			        for (String img: data) {
			        	newInterest.addImage(img); 
			        }
			        for (String url: data2) {
			        	newInterest.addURL(url); 
			        }
			        newInterest.setId(element.getId());
			        parent.getServiceInterest().updateInterest(newInterest);
				    parent.EditMode(false);
				    
				    parent.setAddMode(false);
				    parent.setSelectedElement(null);
				    parent.reload();
				    parent.loadInterest();
				}
			});
			btnPlus.setFocusPainted(false);
			this.add(btnSave, gc);
			//-
			gc.gridwidth = 2;
			gc.gridheight = 1;
			gc.gridx=2;
			gc.gridy=12;
			JButton btnCancel = new JButton("Annuler");
			btnCancel.addActionListener(new ActionListener() {	
				@Override
				public void actionPerformed(ActionEvent arg0) {
					parent.EditMode(false);
					parent.reload();
				}
			});
			btnPlus.setFocusPainted(false);
			this.add(btnCancel, gc);
			
			
			
			
			
			this.revalidate();
		}
		
		public void testAdd(String y){
			String dataTemp[] = new String[data.length+1];	
			System.arraycopy(data, 0, dataTemp, 0, data.length);
			dataTemp[data.length]=y;
			data = dataTemp;
		}
		public void AddUrl(String x){
			String dataTemp[] = new String[data2.length+1];	
			System.arraycopy(data2, 0, dataTemp, 0, data2.length);
			dataTemp[data2.length]=x;
			data2 = dataTemp;
			fillPanel();
		}
}
